﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using XCI.Core;
using XCI.Windows;
using XCI.Sys.Filter;
using XCI.Sys.Model;

namespace XCI.Sys.Service
{
    /// <summary>
    /// 系统序列服务
    /// </summary>
    public class SysSeqService
    {
        private const string RootUrl = "/api/sys/seq";
        public static SysSeqService Instance { get; } = new SysSeqService();

        /// <summary>
        /// 新建序列
        /// </summary>
        public async Task<SysSeq> NewAsync()
        {
            return await Task.FromResult(new SysSeq
            {
                StartWith = 1,
                CurrentValue = 1,
                IncrementBy = 1
            });
        }

        /// <summary>
        /// 复制序列
        /// </summary>
        /// <param name="id">待复制主键</param>
        public async Task CopyAsync(string id)
        {
            var model = await SelectByIdAsync(id);
            model.Id = null;
            model.Name += " - 复制";
            model.Code += "copy";
            await SaveAsync(true, model);
        }

        /// <summary>
        /// 检查序列编码是否存在
        /// </summary>
        /// <param name="code">序列编码</param>
        public async Task<bool> ExistByCodeAsync(string code)
        {
            return await App.Client.PostAsync<bool>($"{RootUrl}/existByCode", new { code });
        }

        /// <summary>
        /// 保存序列
        /// </summary>
        /// <param name="created">是否新增</param>
        /// <param name="model">序列对象</param>
        public async Task<BoolResult> SaveAsync(bool created, SysSeq model)
        {
           return await App.Client.PostAsBoolResultAsync($"{RootUrl}/{(created ? "insert" : "update")}", model);
        }

        /// <summary>
        /// 删除序列
        /// </summary>
        /// <param name="ids">主键字符串</param>
        /// <returns></returns>
        public async Task DeleteAsync(string ids)
        {
            await App.Client.PostAsync($"{RootUrl}/delete", new { ids });
        }

        /// <summary>
        /// 根据主键获取序列对象
        /// </summary>
        /// <param name="id">序列主键</param>
        public async Task<SysSeq> SelectByIdAsync(string id)
        {
            return await App.Client.PostAsync<SysSeq>($"{RootUrl}/selectById", new { id });
        }

        /// <summary>
        /// 根据主键获取序列对象
        /// </summary>
        /// <param name="args">编辑参数</param>
        public Task<SysSeq> GetOrCreateAsync(EditFormArgs args)
        {
            if (args == null || args.Created) return NewAsync();
            return SelectByIdAsync(args.Id);
        }

        /// <summary>
        /// 根据序列编码获取序列对象
        /// </summary>
        /// <param name="code">序列编码</param>
        public async Task<SysSeq> SelectByCodeAsync(string code)
        {
            return await App.Client.PostAsync<SysSeq>($"{RootUrl}/selectByCode", new { code });
        }

        /// <summary>
        /// 根据编码查询当前序列值
        /// </summary>
        /// <param name="code">序列编码</param>
        public async Task<long> SelectCurrentValueAsync(string code)
        {
            return await App.Client.PostAsync<long>($"{RootUrl}/selectCurrentValue", new { code });
        }

        /// <summary>
        /// 根据编码查询下一个序列值
        /// </summary>
        /// <param name="code">序列编码</param>
        public async Task<long> SelectNextValueAsync(string code)
        {
            return await App.Client.PostAsync<long>($"{RootUrl}/selectNextValue", new { code });
        }

        /// <summary>
        /// 查询序列分页列表
        /// </summary>
        /// <param name="filter">过滤对象</param>
        public async Task<PagingList<SysSeq>> SelectPageListAsync(SysSeqFilter filter)
        {
            return await App.Client.PostAsync<PagingList<SysSeq>>($"{RootUrl}/selectPageList", filter);
        }

        /// <summary>
        /// 查询序列列表
        /// </summary>
        /// <param name="filter">过滤对象</param>
        public async Task<List<SysSeq>> SelectListAsync(SysSeqFilter filter)
        {
            return await App.Client.PostAsync<List<SysSeq>>($"{RootUrl}/selectList", filter);
        }

        /// <summary>
        /// 导出序列列表
        /// </summary>
        /// <param name="filePath">文件保存路径</param>
        /// <param name="filter">过滤对象</param>
        public async Task ExportAsync(string filePath, SysSeqFilter filter)
        {
            await App.Client.DownloadAsync($"{RootUrl}/export", filePath, filter);
        }
    }
}